﻿#include "VideoRenderer.h"

VideoRenderer::VideoRenderer()
    : QQuickImageProvider(QQuickImageProvider::Texture)
{

}

void VideoRenderer::updateFrame(const QImage &frame) {
    QMutexLocker lock(&m_frameMutex);
    m_currentFrame = frame;
    emit textureUpdated();
}

QImage VideoRenderer::requestImage(const QString &id, QSize *size, const QSize &requestedSize) {
    Q_UNUSED(id)
    Q_UNUSED(requestedSize)

    QMutexLocker lock(&m_frameMutex);
    if(size) *size = m_currentFrame.size();
    return m_currentFrame;
}

QQuickTextureFactory* VideoRenderer::requestTexture(const QString &id, QSize *size, const QSize &requestedSize) {
    Q_UNUSED(id)
    Q_UNUSED(requestedSize)

    QMutexLocker lock(&m_frameMutex);
    if(m_currentFrame.isNull()){
        return nullptr;
    }

    if(size) *size = m_currentFrame.size();
    return QQuickTextureFactory::textureFactoryForImage(m_currentFrame);
}
